<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" width="50%" :close-on-click-modal="false" :close-on-press-escape	='false'
               :title="form.id?'编辑服务器':'新增接收服务器'" center>
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="130px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入服务器名称"></el-input>
        </el-form-item>
        <el-form-item label="pop3服务器" prop="popServer">
          <el-input v-model="form.popServer" placeholder="请输入pop3服务器"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="pop服务端口号" prop="port">
          <el-input v-model="form.port" placeholder="请输入pop服务端口号"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮-->
      <div slot="footer" class="dialog-footer" align="right">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini"  @click="save" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {add, update} from "@/api/mail/admin/mail-set";
    export default {
        name: "MailSetPut",
        data() {
            return {
                dialogVisible: false,
                form: {
                    id:'',
                    name: "", //名称
                    popServer: "", //pop3服务器
                    userName: "", //用户名
                    port: '', //端口号
                    // emailAdd: "", //邮箱地址
                    // userPassWord: "", //用户密码
                    // fileGuid: "", //文件GUID
                    // popServerType: 0, //pop服务类型
                    // proxyPassWord: "", //代理服务器密码
                    // proxyPort: 0, //代理服务器端口号
                    // proxyServer: "", //代理服务器地址
                    // proxyType: 0, //代理服务器类型
                    // proxyUserName: "", //代理服务器用户名
                    // recType: 0, //邮件接收方式(客户端接受，服务器接收)
                    // isEnable: "", //是否启用
                    // isSSL: "", //IsSSL加密
                    // isStartTls: "", //是否使用StartTls传输
                    // isUserProxyServer: "", //是否使用代理服务器
                    // isUserSpa: "", //是否使用安全密码验证
                },
                rules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ],
                    popServer: [
                        {required: true, message: '请输入pop3服务器', trigger: 'blur'}
                    ],
                    userName: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ],
                    port: [
                        {required: true, message: '请输入pop服务端口号', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(row) {
                this.dialogVisible = true;
                if(JSON.stringify(row)!=='{}'){ //编辑
                    this.form = Object.assign({},row);
                }else{
                    this.form.name=this.form.popServer=this.form.userName=this.form.port=''
                }
            },
            save() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if(this.form.id){ //编辑更新
                            update(this.form,0).then(res => {
                                this.dealEnd(res)
                            }).catch(err => {
                                console.log(err)
                            });
                        }else{ //新增
                            add(this.form,0).then(res => {
                              this.dealEnd(res)
                            }).catch(err => {
                                console.log(err)
                            });
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res){
                if(res.data.success){
                    this.$refs.ruleForm.resetFields();
                    this.dialogVisible=false;
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                    this.$emit('getData', 0)
                }else{
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }

            }
        }
    }
</script>

<style scoped>

</style>
